﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Vehicle.Web.Pages.Role
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Permissions.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-08-27   Created by 杨光远 V1.00.0000
    /// 功能说明：权限分配页面类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Permissions : Component.PageBase
    {

        #region ---实例化变量---
        BLL.Admin b_Admin = new BLL.Admin();//实例化Admin逻辑层
        BLL.Permissions b_Permissions = new BLL.Permissions();
        BLL.Log b_Log = new Vehicle.BLL.Log();
        BLL.RolePermission b_RolePermission = new BLL.RolePermission();
        #endregion

        #region---基本操作---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                b_Admin.DropDownListBind2(Drop_Select);//绑定下拉控件 
                Hf_R_ID.Value = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : Drop_Select.SelectedValue;//角色ID
                Drop_Select.SelectedValue = Request.QueryString["id"] != null ? Request.QueryString["id"].ToString() : Drop_Select.SelectedValue;
                DatalistBind();


            }
        }
        /// <summary>
        /// 绑定Datalist
        /// </summary>
        public void DatalistBind()
        {
            b_Permissions.DataListBind(Dlst);
        }
        /// <summary>
        /// 提交设置事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Btn_Go_Click(object sender, EventArgs e)
        {
            b_RolePermission.Delete(new Model.ComponentsInfo("RolePermission", "R_ID", Hf_R_ID.Value));//先删除角色所有权限
            foreach (DataListItem item in Dlst.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("Chk_RP_Value");
                if (chk.Checked)
                {

                    b_RolePermission.Add(new Model.RolePermissionInfo(Vehicle.Common.TextHelper.StringToInt(Hf_R_ID.Value), Vehicle.Common.TextHelper.StringToInt(chk.ToolTip)));
                }

            }
            b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功设置角色（" + b_Permissions.GetModel(new Vehicle.Model.ComponentsInfo("Role", "R_ID", Hf_R_ID.Value, "R_ID")).Rows[0]["R_Name"].ToString() + "）的相应权限"));//添加日志

            Vehicle.Common.MessageHelper.AlertRedirect("设置成功", "Main.aspx");

        }
        /// <summary>
        /// 下拉菜单事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Drop_Select_SelectedIndexChanged(object sender, EventArgs e)
        {

            Hf_R_ID.Value = Drop_Select.SelectedValue;
            DatalistBind();
        }
        /// <summary>
        /// DataList数据项绑定事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Dlst_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {

                CheckBox chk = (CheckBox)e.Item.FindControl("Chk_RP_Value");
                if (b_RolePermission.GetModel(new Model.ComponentsInfo("RolePermission", "R_ID", Hf_R_ID.Value, "P_ID", chk.ToolTip, "RP_ID")).DefaultView.Count > 0)
                {
                    chk.Checked = true;
                }
            }
        }
        #endregion
    }
}
